package src.main.zhongjiezhe;

import java.util.ArrayList;
import java.util.List;

public class Test001 {
    public static void main(String[] args) {
        Agency agency = new AgencyImpl();

        Person zhangSan = new Person("张三", 20, 1, agency);
        Person liSi = new Person("李四", 20, 2, agency);
        Person wangWu = new Person("王五", 21, 2, agency);

        liSi.findPerson();
    }
}
/**
 * 婚姻介绍所 接口
 */
interface Agency {
    // 注册会员
    void register(Person person);
    // 为会员配对
    void pair(Person person);
}


/**
 * 参与人员类
 */
class Person {
    // 姓名
    String name;
    // 年龄
    int age;
    // 1为男 2 为女
    int sex;
    // 婚姻介绍所
    Agency agency;

    public Person(String name, int age, int sex, Agency agency) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.agency = agency;
        agency.register(this);
    }

    // 配对方法
    public void findPerson() {
        agency.pair(this);
    }
}



class AgencyImpl implements Agency {
    // 参与者集合
    private List<Person> personList = new ArrayList<>();

    @Override
    public void register(Person person) {
        personList.add(person);
    }

    @Override
    public void pair(Person person) {
        for (Person value : personList) {
            System.out.println(person.age +"+"+ value.age +"+"+ person.sex +"+"+ value.sex);
            if (person.age == value.age && person.sex != value.sex ) {
                System.out.println("配对成功：" + value.name);
            }
        }
    }
}




